
#include <QtGlobal>
#include <QFile>
#include <QtCore>
#include <QDataStream>
#include "fileoperations.h"

const char *SLEPItem::TYPES_LONG[] =   {"NONE",
                                    "Single value",
                                    "Dense matrix",
                                    "Sparse matrix IJV Format",
                                    "Sparse matrix Compressed Sparse Column Format",
                                    "Sparse matrix Compressed Sparse Row Format",
                                    "Dense vector",
                                    "Sparse vector",
									"Group Indices"
                                    };

const char *SLEPItem::TYPES_SHORT[] ={ "NONE",
                                    "sca",
                                    "Mat",
                                    "SpMatIJV",
                                    "SpMatCSC",
                                    "SpMatCSR",
                                    "Vec",
                                    "SpVec",
									"Grp"
                                    };


SLEPItem FileOperations::readFile(const QString& filename){
    SLEPItem item;
    QFile file(filename);
    char filetype[4];
    int size1, size2;
    item.type = SLEPItem::NONE;
    if(! file.exists())
        return item;
    if(! file.open(QIODevice::ReadOnly))
        return item;
    QDataStream stream(&file);

    if( (QFileInfo(file).suffix().compare("mtx",Qt::CaseInsensitive)==0)
                || (QFileInfo(file).suffix().compare("mtb",Qt::CaseInsensitive)==0)
                || (QFileInfo(file).suffix().compare("matb",Qt::CaseInsensitive)==0) ){
        stream.readRawData(filetype, sizeof(char) * 3);
        filetype[3] = '\0';
        if(QString(filetype).compare("Mat",Qt::CaseInsensitive)==0){
                item.type = SLEPItem::DENSE_MATRIX;
                stream >>  (item.m);
                stream >>  (item.n);
                item.data = new double[item.m * item.n];
                size1 = sizeof(double) * item.m * item.n ;
                size2 = stream.readRawData( ( char *) item.data, sizeof(double) * item.m * item.n);
                if(size1 != size2){
                        delete item.data;
                        item.type = SLEPItem::NONE;
                        return item;
                }
        }else if(QString(filetype).compare("IJV",Qt::CaseInsensitive)==0){
                item.type = SLEPItem::IJV_MATRIX;
                stream >>  item.nnz;
                item.data = new double[item.nnz];
                item.ijvmat_i = new int[item.nnz];
                item.ijvmat_j = new int[item.nnz];
                size1 = sizeof(int) * item.nnz;
                size2 = stream.readRawData( (char*) item.ijvmat_i, sizeof(int) * item.nnz);
                if(size1 != size2){
                        delete item.ijvmat_i;
                        item.type = SLEPItem::NONE;
                        return item;
                }
                size2 = stream.readRawData( (char*) (item.ijvmat_j), sizeof(int) * item.nnz);
                if(size1 != size2){
                        delete item.ijvmat_i;
                        delete item.ijvmat_j;
                        item.type = SLEPItem::NONE;
                        return item;
                }
                size1 = sizeof(double) * item.nnz;
                size2 = stream.readRawData( (char*) (item.data), sizeof(double) * item.nnz);
                if(size1 != size2){
                        delete item.ijvmat_i;
                        delete item.ijvmat_j;
                        delete item.data;
                        item.type = SLEPItem::NONE;
                        return item;
                }
        }else if(QString(filetype).compare("csc",Qt::CaseInsensitive)==0){
                item.type = SLEPItem::SPARSE_MATRIX_CSC;
                stream >> item.n;
                item.cscmat_cols = new int[item.n + 1];
                size1 = (item.n + 1)  * sizeof(int);
                size2 = stream.readRawData( (char*) (item.cscmat_cols), (item.n + 1)  * sizeof(int));
                if(size1 != size2){
                        delete item.cscmat_cols;
                        item.type = SLEPItem::NONE;
                        return item;
                }
                item.nnz = item.cscmat_cols[item.n];
                item.cscmat_rowindex = new int[item.nnz];
                size1 = item.nnz * sizeof(int);
                size2 = stream.readRawData( (char*) (item.cscmat_rowindex), item.nnz * sizeof(int));
                if(size1 != size2){
                        delete item.cscmat_cols;
                        delete item.cscmat_rowindex;
                        item.type = SLEPItem::NONE;
                        return item;
                }
                item.data = new double[item.nnz];
                size1 = item.nnz * sizeof(double);
                size2 = stream.readRawData( (char*) (item.data), item.nnz * sizeof(double));
                if(size1 != size2){
                        delete item.cscmat_cols;
                        delete item.cscmat_rowindex;
                        delete item.data;
                        item.type = SLEPItem::NONE;
                        return item;
                }
        }else if(QString(filetype).compare("csr",Qt::CaseInsensitive)==0){
                item.type = SLEPItem::SPARSE_MATRIX_CSR;
                stream >> item.m;
                item.csrmat_rows = new int[item.m + 1];
                size1 = (item.m + 1)  * sizeof(int);
                size2 = stream.readRawData( (char*) (item.csrmat_rows), (item.m + 1)  * sizeof(int) );
                if(size1 != size2){
                        delete item.csrmat_rows;
                        item.type = SLEPItem::NONE;
                        return item;
                }
                item.nnz = item.csrmat_rows[item.m];
                item.csrmat_colindex = new int[item.nnz];
                size1 = item.nnz * sizeof(int);
                size2 = stream.readRawData( (char*) (item.csrmat_colindex), item.nnz * sizeof(int));
                if(size1 != size2){
                        delete item.csrmat_rows;
                        delete item.csrmat_colindex;
                        item.type = SLEPItem::NONE;
                        return item;
                }
                item.data = new double[item.nnz];
                size1 = item.nnz * sizeof(double);
                size2 = stream.readRawData( (char*) (item.data), item.nnz * sizeof(double));
                if(size1 != size2){
                        delete item.csrmat_rows;
                        delete item.csrmat_colindex;
                        delete item.data;
                        item.type = SLEPItem::NONE;
                        return item;
                }
        }else{
            file.close();
            return item;
        }
    }else if( (QFileInfo(file).suffix().compare("vctb",Qt::CaseInsensitive)==0)
            ||	(QFileInfo(file).suffix().compare("vcb",Qt::CaseInsensitive)==0)
            ||	(QFileInfo(file).suffix().compare("vecb",Qt::CaseInsensitive)==0)){
        stream.readRawData(filetype,sizeof(char) * 3);
        filetype[3] = '\0';
        if(QString(filetype).compare("Vct",Qt::CaseInsensitive)==0){
                item.type = SLEPItem::DENSE_VECTOR;
                stream >> item.dimension;
                item.data = new double[ item.dimension];
                size1 = sizeof(double) * item.dimension;
                size2 = stream.readRawData( (char *) (item.data), sizeof(double) * item.dimension );
                if(size1 != size2){
                        delete item.data;
                        item.type = SLEPItem::NONE;
                        return item;
                }
        }else if(QString(filetype).compare("spv",Qt::CaseInsensitive)==0){
                item.type = SLEPItem::SPARSE_VECTOR;
                stream >> item.nnz;
                item.spvec_index = new int[item.nnz];
                size1 = sizeof(int)  * item.nnz;
                size2 = stream.readRawData((char*) (item.spvec_index), sizeof(int)  * item.nnz);
                if(size1 != size2){
                        delete item.spvec_index;
                        item.type = SLEPItem::NONE;
                        return item;
                }
                item.data = new double[item.nnz];
                size1 = sizeof(double) * item.nnz;
                size2 = stream.readRawData( (char*) (item.data), sizeof(double) * item.nnz);
                if(size1 != size2){
                        delete item.spvec_index;
                        delete item.data;
                        return item;
                }
        }
    }
    file.close();
    return item;
}


bool FileOperations::writeFile(SLEPItem item, QString& filename){
        if( item.type == SLEPItem::DENSE_VECTOR || item.type == SLEPItem::SPARSE_VECTOR){
                if( ! (filename.endsWith(".vctb") || filename.endsWith(".vcb") || filename.endsWith(".vecb")))
                        filename += ".vcb";
        }else{
                if( ! (filename.endsWith(".mtx") || filename.endsWith(".mtb") || filename.endsWith(".matb")))
                        filename += ".mtx";
        }
    QFile file(filename);
    int size1, size2;
    file.open(QIODevice::WriteOnly);
    QDataStream stream(&file);
        if( item.type == SLEPItem::DENSE_MATRIX ){
                stream << (quint8)'M' << (quint8)'a' << (quint8)'t';
                stream << (qint32) item.m;
                stream << (qint32) item.n;
                size1 = item.m * item.n * sizeof(double);
                size2 = stream.writeRawData( (char*) item.data,  item.m * item.n * sizeof(double));
                if(size1 != size2){
                        file.close();
                        return false;
                }
        }else if( item.type == SLEPItem::IJV_MATRIX){
                stream << (quint8)'I' << (quint8)'J' << (quint8)'V';
                stream << (qint32) item.nnz;
                size1 = item.nnz * sizeof(int);
                size2 = stream.writeRawData( (char*) item.ijvmat_i, item.nnz * sizeof(int));
                if( size1 != size2) {
                        file.close();
                        return false;
                }
                size2 = stream.writeRawData( (char*) item.ijvmat_j, item.nnz * sizeof(int));
                if( size1 != size2) {
                        file.close();
                        return false;
                }
                size1 = item.nnz * sizeof(double);
                size2 = stream.writeRawData( (char*) item.data, item.nnz * sizeof(double));
                if( size1 != size2) {
                        file.close();
                        return false;
                }
        }else if( item.type == SLEPItem::SPARSE_MATRIX_CSC){
                stream << (quint8)'C' << (quint8)'S' << (quint8) 'C';
                stream << (qint32) item.n;
                size1 = (1+item.n) * sizeof(int);
                size2 = stream.writeRawData( (char*) item.cscmat_cols, (1+item.n) * sizeof(int));
                if( size1 != size2 ){
                        file.close();
                        return false;
                }
                size1 = sizeof(int) * item.cscmat_cols[item.n];
                size2 = stream.writeRawData( (char*) item.cscmat_rowindex, sizeof(int) * item.cscmat_cols[item.n]);
                if( size1 != size2 ){
                        file.close();
                        return false;
                }
                size1 = sizeof(double) * item.cscmat_cols[item.n];
                size2 = stream.writeRawData( (char*) item.data, sizeof(double) * item.cscmat_cols[item.n]);
                if( size1 != size2 ){
                        file.close();
                        return false;
                }
        }else if( item.type == SLEPItem :: SPARSE_MATRIX_CSR){
                stream << (quint8)'C' << (quint8)'S' << (quint8) 'R';
                stream << (qint32) item.m;
                size1 = (1+item.m) * sizeof(int);
                size2 = stream.writeRawData( (char*) item.csrmat_rows, (1+item.m) * sizeof(int));
                if( size1 != size2 ){
                        file.close();
                        return false;
                }
                size1 = sizeof(int) * item.csrmat_rows[item.m];
                size2 = stream.writeRawData( (char*) item.csrmat_colindex, sizeof(int) * item.csrmat_rows[item.m]);
                if( size1 != size2 ){
                        file.close();
                        return false;
                }
                size1 = sizeof(double) * item.csrmat_rows[item.m];
                size2 = stream.writeRawData( (char*) item.data, sizeof(double) * item.csrmat_rows[item.m]);
                if( size1 != size2 ){
                        file.close();
                        return false;
                }
        }else if( item.type == SLEPItem :: DENSE_VECTOR){
                stream << (quint8)'v' << (quint8)'c' << (quint8)'t';
                stream << (qint32) item.dimension;
                size1 = item.dimension * sizeof(double);
                size2 = stream.writeRawData( (char*) item.data, item.dimension * sizeof(double));
                if(size1 != size2){
                        file.close();
                        return false;
                }
        }else if( item.type == SLEPItem::SPARSE_VECTOR){
                stream << (quint8)'s' << (quint8)'p' << (quint8)'v';
                stream << (qint32) item.nnz;
                size1 = item.nnz * sizeof(int);
                size2 = stream.writeRawData( (char*) item.spvec_index, item.nnz * sizeof(int));
                if(size1 != size2){
                        file.close();
                        return false;
                }
                size1 = item.nnz * sizeof(double);
                size2 = stream.writeRawData( (char*) item.data, item.nnz * sizeof(double));
                if(size1 != size2){
                        file.close();
                        return false;
                }
        }
        file.close();
        return true;
}





SLEPItem FileOperations::readTextFile(const QString& filename){
    SLEPItem item;
    QFile file(filename);
    int i, j;
    item.type = SLEPItem::NONE;
    if(! file.exists())
        return item;
    if(! file.open(QIODevice::ReadOnly|QIODevice::Text))
        return item;
        QTextStream stream(&file);

        if( QFileInfo(file).suffix().compare("ijv",Qt::CaseInsensitive)==0){
                stream >> item.nnz ;
                item.ijvmat_i = new int[item.nnz];
                item.ijvmat_j = new int[item.nnz];
                item.data  = new double[item.nnz];
                for(i=0; i < item.nnz; i++)
                        stream >> item.ijvmat_i[i];
                for(i=0; i < item.nnz; i++)
                        stream >> item.ijvmat_j[i];
                for(i=0; i < item.nnz; i++)
                        stream >> item.data[i];
                item.type = SLEPItem::IJV_MATRIX;
        }else if( (QFileInfo(file).suffix().compare("csc",Qt::CaseInsensitive)==0)
                || (QFileInfo(file).suffix().compare("ccs",Qt::CaseInsensitive)==0) ){
                stream >> item.n ;
                item.cscmat_cols = new int[1 + item.n];
                for(i=0; i <= item.n; i++)
                        stream >> item.cscmat_cols[i];
                item.cscmat_rowindex = new int[item.cscmat_cols[item.n]];
                item.data  = new double[item.cscmat_cols[item.n]];
                for(i=0; i < item.cscmat_cols[item.n]; i++)
                        stream >> item.cscmat_rowindex[i];
                for(i=0; i < item.cscmat_cols[item.n]; i++)
                        stream >> item.data[i];
                item.type = SLEPItem::SPARSE_MATRIX_CSC;
        }else if( (QFileInfo(file).suffix().compare("csr",Qt::CaseInsensitive)==0)
                || (QFileInfo(file).suffix().compare("crs",Qt::CaseInsensitive)==0) ){
                stream >> item.m ;
                item.csrmat_rows = new int[1 + item.m ];
                for(i=0; i <= item.m; i++)
                        stream >> item.csrmat_rows[i];
                item.csrmat_colindex = new int[item.csrmat_rows[item.m]];
                item.data  = new double[item.csrmat_rows[item.m]];
                for(i=0; i < item.csrmat_rows[item.m]; i++)
                        stream >> item.csrmat_colindex[i];
                for(i=0; i < item.csrmat_rows[item.m]; i++)
                        stream >> item.data[i];
                item.type = SLEPItem::SPARSE_MATRIX_CSR;
        }else if( QFileInfo(file).suffix().compare("spv",Qt::CaseInsensitive)==0){
                stream >> item.nnz ;
                item.spvec_index = new int[item.nnz];
                item.data  = new double[item.nnz];
                for(i=0; i < item.nnz; i++)
                        stream >> item.spvec_index[i];
                for(i=0; i < item.nnz; i++)
                        stream >> item.data[i];
                item.type = SLEPItem::SPARSE_VECTOR;
        }else if( (QFileInfo(file).suffix().compare("vec",Qt::CaseInsensitive)==0)
                || (QFileInfo(file).suffix().compare("vct",Qt::CaseInsensitive)==0)
                || (QFileInfo(file).suffix().compare("vctr",Qt::CaseInsensitive)==0) ){
                double val;
                item.dimension = 0;
                while(!stream.atEnd()){
                        stream >> val;
                        stream.skipWhiteSpace();
                        (item.dimension)++;
                }
                item.data = new double[item.dimension];
                stream.seek(0);
                for(i=0; i< item.dimension; i++)
                        stream >> item.data[i];
                item.type = SLEPItem::DENSE_VECTOR;
        }else if( QFileInfo(file).suffix().compare("mat",Qt::CaseInsensitive)==0){
                QString line;
                double temp;
                item.m=0;
                item.n=0;
                if(stream.atEnd())
                        return item;
                line = stream.readLine();
                (item.m)++;
                QTextStream lineStream(&line);
                while(!lineStream.atEnd()){
                        lineStream >> temp;
                        lineStream.skipWhiteSpace();
                        (item.n)++;
                }
                while(!stream.atEnd()){
                        line = stream.readLine();
                        item.m++;
                }
                item.data = new double[ item.m * item.n ];
                stream.seek(0);
                for(i=0; i< item.m; i++)
                        for(j=0; j< item.n; j++)
                                stream >> item.data[j*item.m + i];
                item.type = SLEPItem::DENSE_MATRIX;
		}else if( QFileInfo(file).suffix().compare("grp",Qt::CaseInsensitive)==0){
			stream >> item.group_size;
			item.group_index = new int[item.group_size];
			for(i=0; i < item.group_size; i++)
				stream >> item.group_index[i];
			item.type = SLEPItem::GROUP_INDICES;
		}else{
                QString line;
                double temp;
                item.m=0;
                item.n=0;
                if(stream.atEnd())
                        return item;
                line = stream.readLine();
                (item.m)++;
                QTextStream lineStream(&line);
                while(!lineStream.atEnd()){
                        lineStream >> temp;
                        lineStream.skipWhiteSpace();
                        (item.n)++;
                }
                while(!stream.atEnd()){
                        line = stream.readLine();
                        item.m++;
                }
                item.data = new double[ item.m * item.n ];
                stream.seek(0);
                for(i=0; i< item.m; i++)
                        for(j=0; j< item.n; j++)
                                stream >> item.data[j*item.m + i];
                if(item.m==1 || item.n==1){
                        item.dimension = (item.m > item.n) ? item.m : item.n;
                        item.type = SLEPItem::DENSE_VECTOR;
                }else
                        item.type = SLEPItem::DENSE_MATRIX;
        }
    file.close();
    return item;
}



bool FileOperations::writeTextFile(SLEPItem item, QString &filename){
        int i,j;
        if( item.type == SLEPItem::DENSE_MATRIX ){
                if( ! filename.endsWith(".mat"))
                        filename += ".mat";
                QFile file(filename);
                if(!file.open(QIODevice::WriteOnly| QIODevice::Text))
                        return false;
                QTextStream stream(&file);
                for(i=0; i< item.m; i++){
                        for(j=0; j<(item.n-1); j++)
                                stream << item.data[j*item.m + i]<< " ";
                        stream << item.data[j*item.m + i] << "\n";
                }
                file.close();
                return true;
        }else if( item.type == SLEPItem::IJV_MATRIX){
                if( ! filename.endsWith(".ijv"))
                        filename += ".ijv";
                QFile file(filename);
                file.open(QIODevice::WriteOnly| QIODevice::Text);
                QTextStream stream(&file);
                stream << item.nnz<< "\n";
                for(i=0; i< (item.nnz-1); i++)
                        stream << item.ijvmat_i[i] << " ";
                stream << item.ijvmat_i[i] << "\n";

                for(i=0; i< (item.nnz-1); i++)
                        stream << item.ijvmat_j[i] << " ";
                stream << item.ijvmat_j[i] << "\n";

                for(i=0; i< (item.nnz-1); i++)
                        stream << item.data[i] << " ";
                stream << item.data[i] << "\n";

                file.close();
                return true;
        }else if( item.type == SLEPItem::SPARSE_MATRIX_CSC){
                int nnz;
                if( ! (filename.endsWith(".csc") || filename.endsWith(".ccs")))
                        filename += ".csc";
                QFile file(filename);
                file.open(QIODevice::WriteOnly| QIODevice::Text);
                QTextStream stream(&file);
                stream << item.n << "\n";
                for(i=0; i< item.n; i++)
                        stream << item.cscmat_cols[i] << " ";
                stream << item.cscmat_cols[i] << "\n";

                nnz = item.cscmat_cols[i];
                for(i=0; i< (nnz-1); i++)
                        stream << item.cscmat_rowindex[i] << " ";
                stream << item.cscmat_rowindex[i] << "\n";

                for(i=0; i< (nnz-1); i++)
                        stream << item.data[i] << " ";
                stream << item.data[i] << "\n";

                file.close();
                return true;
        }else if( item.type == SLEPItem :: SPARSE_MATRIX_CSR){
                int nnz;
                if( ! (filename.endsWith(".csr") || filename.endsWith(".crs")))
                        filename += ".csr";
                QFile file(filename);
                file.open(QIODevice::WriteOnly| QIODevice::Text);
                QTextStream stream(&file);
                stream << item.m << "\n";
                for(i=0; i< item.m; i++)
                        stream << item.csrmat_rows[i] << " ";
                stream << item.csrmat_rows[i] << "\n";

                nnz = item.cscmat_cols[i];
                for(i=0; i< (nnz-1); i++)
                        stream << item.csrmat_colindex[i] << " ";
                stream << item.csrmat_colindex[i] << "\n";

                for(i=0; i< (nnz-1); i++)
                        stream << item.data[i] << " ";
                stream << item.data[i] << "\n";

                file.close();
                return true;
        }else if( item.type == SLEPItem :: DENSE_VECTOR){
                if( ! (filename.endsWith(".vec") || filename.endsWith(".vct") || filename.endsWith(".vctr")))
                        filename += ".vec";
                QFile file(filename);
                file.open(QIODevice::WriteOnly| QIODevice::Text);
                QTextStream stream(&file);
                for(i=0; i < item.dimension; i++)
                        stream << item.data[i]<< "\n";
                file.close();
                return true;
        }else if( item.type == SLEPItem::SPARSE_VECTOR){
                if( ! (filename.endsWith(".spv")) )
                        filename += ".spv";
                QFile file(filename);
                file.open(QIODevice::WriteOnly| QIODevice::Text);
                QTextStream stream(&file);
                stream << item.nnz << "\n";
                for(i=0; i<(item.nnz-1); i++)
                        stream << item.spvec_index[i]<< " ";
                stream << item.spvec_index[i]<< "\n";
                for(i=0; i<(item.nnz-1); i++)
                        stream << item.data[i]<< " ";
                stream << item.data[i]<< "\n";
                file.close();
                return true;
		}else if( item.type == SLEPItem::GROUP_INDICES){
                if( ! (filename.endsWith(".grp")) )
                        filename += ".grp";
                QFile file(filename);
                file.open(QIODevice::WriteOnly| QIODevice::Text);
                QTextStream stream(&file);
				stream << item.group_size << "\n";
                for(i=0; i<(item.group_size-1); i++)
					stream << item.group_index[i]<< " ";
                stream << item.group_index[i]<< "\n";
                file.close();
                return true;
        }
        return false;
}



SLEPItem::SLEPItem(){
    type = SLEPItem::NONE;
}

SLEPItem::~SLEPItem(){
}


void SLEPItem::dealloc(){
        if(type == SLEPItem::NONE)
            return;
        if(type == SLEPItem::DENSE_MATRIX || type == DENSE_VECTOR){
            type = SLEPItem::NONE;
            delete data;
            return;
        }
        if(type == SLEPItem::IJV_MATRIX){
            type = SLEPItem::NONE;
            delete ijvmat_i;
            delete ijvmat_j;
            delete data;
            return;
        }
        if(type == SLEPItem::SPARSE_VECTOR){
            type = SLEPItem::NONE;
            delete spvec_index;
            delete data;
            return;
        }
        if(type == SLEPItem::SPARSE_MATRIX_CSC){
            type = SLEPItem::NONE;
            delete cscmat_cols;
            delete cscmat_rowindex;
            delete data;
            return;
        }
        if(type == SLEPItem::SPARSE_MATRIX_CSR){
            type = SLEPItem::NONE;
            delete csrmat_colindex;
            delete csrmat_rows;
            delete data;
            return;
        }
		if(type == SLEPItem::GROUP_INDICES){
			type = SLEPItem::NONE;
			delete group_index;
			return;
		}
}


QString SLEPItem::info(){
    if(type == SLEPItem::NONE)
        return QString("NONE");
    if(type == SLEPItem::SINGLE_VALUE)
        return QString("1x1");
    if(type == SLEPItem::DENSE_MATRIX)
        return QString("%1x%2 matrix").arg(m).arg(n);
    if(type == SLEPItem::DENSE_VECTOR)
        return QString("%1 dimension").arg(dimension);
    if(type == SLEPItem::SPARSE_VECTOR)
        return QString("%1 non-zero elements").arg(nnz);
    if(type == SLEPItem::IJV_MATRIX)
        return QString("%1 non-zero elements").arg(nnz);
    if(type == SLEPItem::SPARSE_MATRIX_CSC)
        return QString("%1 columns, %2 non-zero elements").arg(n).arg(cscmat_cols[n]);
    if(type == SLEPItem::SPARSE_MATRIX_CSR)
        return QString("%1 rows, %2 non-zero elements").arg(m).arg(csrmat_rows[m]);
	if(type == SLEPItem::GROUP_INDICES)
		return QString("%1 Group").arg(group_size);
    return QString("NONE");
}



SLEPItem*	FileOperations::slepMatrix2SlepItem(slep_matrix* matrix){
	// TODO implement for sparse matrix as well
	SLEPItem *item = new SLEPItem;
	item->type = SLEPItem::NONE;
	if(matrix == 0)
		return item;
	if(matrix->sparsity == SlepDense){
		item->type = SLEPItem::DENSE_MATRIX;
		slep_matrix *copyMat = slepCopyToNewMatrix(matrix);
		item->m = copyMat->m;
		item->n = copyMat->n;
		item->data = copyMat->data.mtrx;
		free(copyMat);
	}
	return item;
}

SLEPItem*	FileOperations::slepVector2SlepItem(slep_vector* vector){
	// TODO implement for sparse as well
	SLEPItem *item = new SLEPItem;
	item->type = SLEPItem::NONE;
	if(vector == 0)
		return item;
	if(vector->sparsity == SlepDense){
		item->type = SLEPItem::DENSE_VECTOR;
		slep_vector* copyVec = slepCopyToNewVector(vector);
		item->dimension = copyVec->dim;
		item->data = copyVec->data.vctr;
	}
	return item;
}

slep_matrix*	FileOperations::slepItem2SlepMatrix(SLEPItem* item){
	// TODO implement for sparse as well
	if(item->type != SLEPItem::DENSE_MATRIX)
		return 0;
	if(item->type == SLEPItem::DENSE_MATRIX){
		slep_matrix matrix;
		matrix.data.mtrx = item->data;
		matrix.m = item->m;
		matrix.n = item->n;
		matrix.ld = item->m;
		matrix.sparsity = SlepDense;
		return slepCopyToNewMatrix(&matrix);
	}
	return 0;

}

slep_vector*	FileOperations::slepItem2SlepVector(SLEPItem* item){
	// TODO implement for sparse as well
	if(item->type != SLEPItem::DENSE_VECTOR)
		return 0;
	if(item->type == SLEPItem::DENSE_VECTOR){
		slep_vector vector;
		vector.data.vctr = item->data;
		vector.dim = item->dimension;
		vector.inc = 1;
		vector.sparsity = SlepDense;
		return slepCopyToNewVector(&vector);
	}
	return 0;
}